// pages/my/my.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    headImg: "",
    name: ""
  },

  data: {
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    orderItems: [
      {
        typeId: 0,
        name: '待付款',
        url: 'bill',
        imageurl: '/images/daifukuan.png',
      },
      {
        typeId: 1,
        name: '待收货',
        url: 'bill',
        imageurl: '/images/daishouhuo.png',
      },
      {
        typeId: 2,
        name: '待评价',
        url: 'bill',
        imageurl: '/images/daipingjia.png'
      },
      {
        typeId: 3,
        name: '退换/售后',
        url: 'bill',
        imageurl: '/images/shouhou.png'
      }
    ],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let self = this;
    let _user = wx.getStorageSync("myUser")
    if (_user) {
      self.setData({
        headImg: _user.avatarUrl,
        name: _user.nickName
      })
    }
  },

  /**
   * 跳转到我的订单页面
   */
  jumpOrder: function() {
    wx.reLaunch({
      url: "/pages/order/order"
    })
  },

  //用户授权
  getUserInfo: function(e) {
    let self = this;
    console.log(e)

    self.setData({
      headImg: e.detail.userInfo.avatarUrl,
      name: e.detail.userInfo.nickName
    })
    // //全局变量赋值
    // app.globalData.userData = e.detail.userInfo
    //同步存储
    wx.setStorageSync("myUser", e.detail.userInfo)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    //todo 
    let self = this;
    let _user = wx.getStorageSync("myUser")
    //如果有值说明已经授权登录过
    if (_user) {
      self.setData({
        headImg: _user.avatarUrl,
        name: _user.nickName
      })
    }

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})